package com.blog.controller.forntend;


import com.blog.entity.User;
import com.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mars123
 * @since 2019-07-02
 */
@Controller
@RequestMapping("/frontend/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(String email, String password, HttpServletRequest request, HttpSession session) {
        if (request.getMethod().equals("POST")) {
            User user = userService.login(email, password, session);
            System.out.println(user);
            System.out.println("controller session:" + session.getAttribute("user"));
            return "redirect:/frontend/product/index";
        } else {
            return "frontend/user/login";
        }
    }

    @RequestMapping("/register")
    public String register(@ModelAttribute("user") User user, HttpServletRequest request, HttpSession session) {
        if (request.getMethod().equals("POST")) {
            userService.register(user, session);
            return "redirect:/frontend/product/index";
        } else {
            return "frontend/user/register";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/frontend/product/index";
    }


}

